package com.samtech.spring.components;

import java.util.List;

import org.apache.commons.net.ftp.FTPFile;
import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.samtech.ftp.service.FtpException;
import com.samtech.ftp.service.impl.CommonFtpServiceImpl;
import com.samtech.tree.FileTreeNode;
@Component("fileTree")
@Scope("request")
public class FileTreeManager {
    private TreeNode root;
    
    
    public FileTreeManager() {   
        /*root = new DefaultTreeNode("Root", null);   
        TreeNode node0 = new DefaultTreeNode("Node 0", root);   
        TreeNode node1 = new DefaultTreeNode("Node 1", root);   
        TreeNode node2 = new DefaultTreeNode("Node 2", root);   
           
        TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);   
        TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);   
           
        TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);   
        TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);   
           
        TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);   
        TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);   
        TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);   
           
        TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10); */  
        CommonFtpServiceImpl ftp = new CommonFtpServiceImpl();
        try {
            ftp.connectServer("172.16.1.41/", "unicomffp", "uni2006@user", false);
            FTPFile file=new FTPFile();
            file.setName("FTP");
            root=new FileTreeNode(file);
            List<FTPFile> fileInfos = ftp.getFileInfos(".");
            FileTreeNode node;
            for (FTPFile ftpFile : fileInfos) {
                 node= new FileTreeNode(ftpFile);
                 root.addChild(node);
            }
        } catch (FtpException e) {
            e.printStackTrace();
        }
    }   
  
    public TreeNode getRoot() {   
        return root;   
    }   
    public void onNodeExpand(NodeExpandEvent event) {   
        String node = event.getTreeNode().getData().toString();   
        //logger.info("Expanded:" + node);   
    }   
       
    public void onNodeCollapse(NodeCollapseEvent event) {   
        String node = event.getTreeNode().getData().toString();   
        //logger.info("Collapsed:" + node);   
    } 
    /**
     * <p:tree value="#{treeBean.root}" var="node" dynamic="true">  
    <p:treeNode>  
        <h:outputText value="#{node}"/>  
    </p:treeNode>  
</p:tree>  

     */
}
